package com.test.demo1.service.impl;

import com.test.demo1.entity.Attachment;
import com.test.demo1.dao.AttachmentDao;
import com.test.demo1.service.AttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import java.util.List;
/**
 *
 * @author huishao
 * @since 2022-11-03 09:22:39
 */
@Service
public class AttachmentServiceImpl extends ServiceImpl<AttachmentDao, Attachment> implements AttachmentService {
    @Resource
    private AttachmentDao attachmentDao;
    
    
    /**
     * 通过 传入字段名返回指定列
     *
     * @param entityName
     * @param entryValue
     * @return Attachment
     */
    @Override
    public Attachment selectByEntity(String entityName, String entryValue) {
        QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(entityName,entryValue);
        queryWrapper.last("limit 1");
        return attachmentDao.selectOne(queryWrapper);
    }

    /**
     * 通过 传入字段键值 返回指定 多列
     *
     * @param entityName
     * @param entryValue
     * @return List<Attachment>
     */
    @Override
    public List<Attachment> listSelectByEntity(String entityName, String entryValue) {
        QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(entityName,entryValue);
        return attachmentDao.selectList(queryWrapper);
    }

    /**
     * 返回全部数据
     *
     * @return List<Attachment>
     */
    @Override
    public List<Attachment> selectAll() {
        return attachmentDao.selectList(new QueryWrapper<>());
    }

    /**
     * 新增
     *
     * @param s Attachment对象
     */
    @Override
    public int add(Attachment s) {
        return attachmentDao.insert(s);
    }

    /**
     * 编辑 Attachment
     *
     * @param s Attachment对象
     * @return int 影响条数
     *
     * 根据业务自己写update ，默认传啥改啥！
     * eg：
     *      User user = new User();
     *      user.setUserId(1);
     *      user.setAge(18);
     *      userMapper.updateById(user);
     */
    @Override
    public int update(Attachment s) {
        return attachmentDao.updateById(s);
    }

    /**
     * 通过字段 删除 Attachment 真删
     *
     * @param entityName
     * @param entryValue
     * @param flag       true 表示真删
     * @return int 影响条数
     */
    @Override
    public int deleteByEntity(String entityName, String entryValue, Boolean flag) {
        if (!flag) {
            UpdateWrapper<Attachment> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq(entityName,entryValue);
            Attachment s = new Attachment();
            // 这里自己处理，isDelete or status
            // s.setDelete(1);
            // s.setStatus(1);
            return attachmentDao.update(s, updateWrapper);
        }else {
            // 真删 ！ 
            QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(entityName,entryValue);
            return attachmentDao.delete(queryWrapper);
        }

    }
    
    
    
    
}
